<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" destroy-on-close :close-on-click-modal="false"
        :before-close="closeDailog" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">
            <el-form-item label="管理员" prop="pkUser">
                <ec-user-select disabled v-if="dataForm.pkUser != ''" userType="group" v-model="dataForm.pkUser"
                    placeholder="管理员"></ec-user-select>
            </el-form-item>
            <el-form-item label="集团" prop="pkGroup">
                <ec-group-tree-select v-model="dataForm.pkGroup" placeholder="集团"></ec-group-tree-select>
            </el-form-item>
            <el-form-item label="生效日期" prop="enabledate">
                <el-date-picker v-model="dataForm.enabledate" style="width:100%" type="date" placeholder="生效日期"
                    format="yyyy-MM-dd" value-format="yyyy-MM-dd">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="失效日期" prop="disabledate">
                <el-date-picker v-model="dataForm.disabledate" style="width:100%" type="date" placeholder="失效日期"
                    format="yyyy-MM-dd" value-format="yyyy-MM-dd">
                </el-date-picker>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as admingroupService from '@/api/upms/admingroup'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkAdminGroup: 0,
                    pkUser: '',
                    pkGroup: '',
                    disabledate: '',
                    enabledate: '',
                    revision: '',
                },
                dataRule: {
                    pkUser: [{
                        required: true,
                        message: '管理员不能为空',
                        trigger: 'change'
                    }],
                    pkGroup: [{
                        required: true,
                        message: '集团不能为空',
                        trigger: 'change'
                    }],
                    disabledate: [{
                        required: true,
                        message: '关联失效日期不能为空',
                        trigger: 'blur'
                    }],
                    enabledate: [{
                        required: true,
                        message: '关联生效日期不能为空',
                        trigger: 'blur'
                    }],
                }
            }
        },
        methods: {
            init(id) {
                this.visible = true
                if (id) {
                    this.dataForm.pkUser = id || ''
                    this.dataForm.pkGroup = ''
                    this.dataForm.disabledate = ''
                    this.dataForm.enabledate = ''
                }
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkAdminGroup': this.dataForm.pkAdminGroup || undefined,
                            'pkUser': this.dataForm.pkUser,
                            'pkGroup': this.dataForm.pkGroup,
                            'disabledate': this.dataForm.disabledate,
                            'enabledate': this.dataForm.enabledate,
                            'revision': this.dataForm.revision
                        }
                        var tick = !this.dataForm.pkAdminGroup ? admingroupService.addadmingroup(params) :
                            admingroupService.updateadmingroup(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>